package com.bitejiuyeke.job_publish.controller;

import com.bitejiuyeke.job_publish.common.AppResult;
import com.bitejiuyeke.job_publish.common.PageResult;
import com.bitejiuyeke.job_publish.config.AppConfig;
import com.bitejiuyeke.job_publish.controller.params.*;
import com.bitejiuyeke.job_publish.enums.JobStateEnum;
import com.bitejiuyeke.job_publish.model.Job;
import com.bitejiuyeke.job_publish.service.IJobService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.constraints.PositiveOrZero;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @Author 比特就业课
 */
// API描述
@Tag(name = "职位操作相关接口")
// 日志
@Slf4j
// 校验
@Validated
// Controller注解，返回的对象用JSON形式表示
@RestController
// 路径映射
@RequestMapping("/job")
public class JobController {

    @Resource
    private IJobService jobService;

    @Operation(summary = "发布职位")
    @PostMapping("/publish")
    public AppResult create (HttpServletRequest request,
                             @Validated @RequestBody JobPublishDTO param) {
        // 获取当前登录用户Id
        Long userId = (Long) request.getAttribute(AppConfig.TOKEN_USER_ID);
        // 定义对象
        Job job = new Job();
        BeanUtils.copyProperties(param, job);
        // 设置用户Id
        job.setUserId(userId);
        // 调用Service
        jobService.create(job);
        // 返回响应
        return AppResult.success();
    }

    @Operation(summary = "修改职位信息")
    @PostMapping("/modify")
    public AppResult modify (HttpServletRequest request,
                             @Validated @RequestBody JobModifyDTO param) {
        // 获取用户Id
        Long userId = (Long) request.getAttribute(AppConfig.TOKEN_USER_ID);
        // 定义对象
        Job job = new Job();
        BeanUtils.copyProperties(param, job);
        // 设置用户Id
        job.setUserId(userId);
        // 调用Service
        jobService.modify(job);
        // 返回结果
        return AppResult.success();
    }

    @Operation(summary = "修改职位状态")
    @PostMapping("/state")
    public AppResult modifyState (HttpServletRequest request,
                                  @Validated @RequestBody JobModifyStateDTO param) {
        // 获取用户Id
        Long userId = (Long) request.getAttribute(AppConfig.TOKEN_USER_ID);
        // 调用Service
        jobService.modifyState(param.getId(), JobStateEnum.valueOf(param.getState()), userId);
        // 返回结果
        return AppResult.success();
    }

    @Operation(summary = "删除职位")
    @DeleteMapping("/delete")
    public AppResult delete (HttpServletRequest request,
                             @PositiveOrZero(message = "请输入职位Id") @RequestParam("id") Long id) {
        // 获取当前登录用户Id
        Long userId = (Long) request.getAttribute(AppConfig.TOKEN_USER_ID);
        // 调用Service
        jobService.deleteById(id, userId);
        return AppResult.success();
    }

    @Operation(summary = "获取职位详情")
    @GetMapping("/details")
    public AppResult<Job> selectById (@PositiveOrZero(message = "请输入职位Id") @RequestParam("id") Long id) {
        // 调用Service
        Job job = jobService.selectById(id);
        if (job == null) {
            return AppResult.failed("没有找到职位信息");
        }
        // 访问量+1
        jobService.addVisitCount(id, 1);
        return AppResult.success(job);
    }

    @Operation(summary = "首页职位列表")
    @GetMapping("/indexPages")
    public AppResult<PageResult<Job>> indexPages (@Validated IndexJobPageDTO param) {
        // 构造请求对象
        Job job = new Job();
        // 复制属性
        BeanUtils.copyProperties(param, job);
        // 调用业务层
        PageResult<Job> pageResult = jobService.selectByPage(job);
        // 返回结果
        return AppResult.success(pageResult);
    }

    @Operation(summary = "用户发布的职位列表")
    @GetMapping("/selectByUser")
    public AppResult<PageResult<Job>> selectByUser (HttpServletRequest request,
                                                    @Validated UserJobPageDTO param) {
        // 获取当前登录用户
        Long userId = (Long) request.getAttribute(AppConfig.TOKEN_USER_ID);
        param.setUserId(userId);
        // 构造请求对象
        Job job = new Job();
        // 复制属性
        BeanUtils.copyProperties(param, job);
        // 调用业务层
        PageResult<Job> pageResult = jobService.selectPageByUser(job);
        // 返回结果
        return AppResult.success(pageResult);
    }

}
